(function () {

    "use strict";

    var api = {
        getOrderDetailUrl: '/api/order/content',
        chgOrder: '/api/order/chgStatus'
    };
    sm.controller('wx-order-success-controller', ['$scope', '$http', 'Modal', 'Toast', function ($scope, $http, Modal, Toast){

        $scope.order = {};

        var search = window.location.search;
        var orderId = search.split('=')[1];

        $http.get(api.getOrderDetailUrl, {
            params: {
                orderId: orderId
            }
        }).success(function (res) {
            if (res.state) {
                $scope.order = res.data;
            }
        });

        /**
         * 评论
         */
        $scope.comment = function () {
            location.href = '/order/comment?orderId=' + GetQueryString('orderId');
        };

        /**
         * 再来一单
         */
        $scope.buy = function () {
            orderId = GetQueryString('orderId');
            if (!orderId) {
                return false;
            }
            Modal.alert({
                title: '操作提示',
                content: '您确定再次购买该产品吗?',
                buttons: [{
                    name: '取消',
                    event: function () {

                    }
                }, {
                    name: '确定购买',
                    event: function () {
                        doBuyMore(orderId);
                    }
                }]
            });
        };

        function doBuyMore(orderId) {
            Toast.loading({title: '请等待...'});
            $http.post(api.buyUrl, {
                orderId: orderId
            }).success(function (res) {
                if (res.state) {
                    location.href = '/order/pay?orderId=' + res.data.orderId;
                } else {
                    Modal.alert({
                        title: '结果提示',
                        content:'对不起, 下单失败',
                        buttons: [{
                            name: '取消',
                            event: function () {

                            }
                        }, {
                            name: '返回订单列表',
                            event: function () {
                                location.href = '/order';
                            }
                        }]
                    });
                }
            });
        }


    }]);
})();

